<template>
	<view class="container">
		<view class="form-box">
			<!-- 信息类型选择 -->
			<view class="type-section">
				<view class="type-tabs">
					<view 
						class="type-tab" 
						:class="{ active: formData.type === 'lost' }"
						@tap="switchType('lost')"
					>
						寻物启事
					</view>
					<view 
						class="type-tab" 
						:class="{ active: formData.type === 'found' }"
						@tap="switchType('found')"
					>
						失物招领
					</view>
				</view>
			</view>

			<!-- 物品信息 -->
			<view class="item-section">
				<view class="form-item">
					<text class="label">物品名称</text>
					<input 
						type="text" 
						v-model="formData.title" 
						:placeholder="formData.type === 'lost' ? '请输入丢失物品名称' : '请输入拾获物品名称'" 
						maxlength="30"
					/>
				</view>

				<view class="form-item">
					<text class="label">物品分类</text>
					<picker mode="selector" :range="categories" @change="onCategoryChange">
						<view class="picker">
							<text>{{formData.category || '请选择物品分类'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>

				<view class="form-item">
					<text class="label">详细描述</text>
					<textarea 
						v-model="formData.description" 
						:placeholder="getDescriptionPlaceholder()" 
						:maxlength="500"
					/>
					<text class="word-count">{{formData.description.length}}/500</text>
				</view>

				<!-- 图片上传 -->
				<view class="form-item">
					<text class="label">相关图片</text>
					<text class="sub-label">（请上传清晰的实物图，最多3张）</text>
					<view class="image-uploader">
						<view class="image-list" v-if="formData.images.length > 0">
							<view class="image-item" v-for="(image, index) in formData.images" :key="index">
								<image :src="image" mode="aspectFill"></image>
								<text class="delete-btn" @tap="deleteImage(index)">×</text>
							</view>
						</view>
						<view class="upload-btn" @tap="chooseImage" v-if="formData.images.length < 3">
							<uni-icons type="camera" size="32" color="#999"></uni-icons>
							<text>上传图片</text>
						</view>
					</view>
				</view>
			</view>

			<!-- 时间地点信息 -->
			<view class="location-section">
				<view class="form-item">
					<text class="label">{{formData.type === 'lost' ? '丢失时间' : '拾获时间'}}</text>
					<picker mode="date" :value="formData.date" @change="onDateChange">
						<view class="picker">
							<text>{{formData.date || '请选择日期'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>

				<view class="form-item">
					<text class="label">{{formData.type === 'lost' ? '丢失地点' : '拾获地点'}}</text>
					<view class="location-select" @tap="selectLocation">
						<text>{{formData.location || '请选择地点'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>

				<view class="form-item">
					<text class="label">联系方式</text>
					<input 
						type="number" 
						v-model="formData.contact" 
						placeholder="请输入手机号码" 
						maxlength="11"
					/>
				</view>

				<view class="form-item">
					<text class="label">悬赏金额</text>
					<view class="price-input-box" v-if="formData.type === 'lost'">
						<text class="currency">¥</text>
						<input 
							type="digit" 
							v-model="formData.reward" 
							placeholder="请输入悬赏金额（选填）" 
							class="price-input"
						/>
					</view>
					<text class="reward-tip" v-else>拾金不昧，无需悬赏</text>
				</view>
			</view>
		</view>

		<!-- 发布按钮 -->
		<view class="submit-btn" @tap="submitForm">
			发布信息
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			categories: ['证件卡片', '电子设备', '钱包钥匙', '书籍资料', '生活用品', '其他'],
			formData: {
				type: 'lost', // lost: 寻物启事, found: 失物招领
				title: '',
				category: '',
				description: '',
				images: [],
				date: '',
				location: '',
				contact: '',
				reward: ''
			}
		}
	},
	methods: {
		switchType(type) {
			this.formData.type = type
			// 切换类型时清空表单
			this.formData.title = ''
			this.formData.description = ''
			this.formData.images = []
			this.formData.date = ''
			this.formData.location = ''
			this.formData.reward = ''
		},
		getDescriptionPlaceholder() {
			return this.formData.type === 'lost' 
				? '请详细描述物品的特征、丢失时的具体情况等信息'
				: '请详细描述物品的特征、拾获时的具体情况等信息'
		},
		onCategoryChange(e) {
			this.formData.category = this.categories[e.detail.value]
		},
		chooseImage() {
			uni.chooseImage({
				count: 1,
				success: (res) => {
					this.formData.images.push(res.tempFilePaths[0])
				}
			})
		},
		deleteImage(index) {
			this.formData.images.splice(index, 1)
		},
		onDateChange(e) {
			this.formData.date = e.detail.value
		},
		selectLocation() {
			uni.showActionSheet({
				itemList: ['教学楼', '图书馆', '食堂', '宿舍楼', '体育馆', '其他'],
				success: (res) => {
					this.formData.location = ['教学楼', '图书馆', '食堂', '宿舍楼', '体育馆', '其他'][res.tapIndex]
				}
			})
		},
		validateForm() {
			if (!this.formData.title) {
				uni.showToast({
					title: '请输入物品名称',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.category) {
				uni.showToast({
					title: '请选择物品分类',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.description) {
				uni.showToast({
					title: '请填写详细描述',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.date) {
				uni.showToast({
					title: this.formData.type === 'lost' ? '请选择丢失时间' : '请选择拾获时间',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.location) {
				uni.showToast({
					title: this.formData.type === 'lost' ? '请选择丢失地点' : '请选择拾获地点',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.contact) {
				uni.showToast({
					title: '请输入联系方式',
					icon: 'none'
				})
				return false
			}
			if (!/^1\d{10}$/.test(this.formData.contact)) {
				uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				})
				return false
			}
			return true
		},
		submitForm() {
			if (!this.validateForm()) return

			// TODO: 发送请求保存信息
			uni.showLoading({
				title: '发布中...'
			})

			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '发布成功',
					icon: 'success'
				})
				// 返回首页
				uni.switchTab({
					url: '/pages/index/index'
				})
			}, 1500)
		}
	}
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: $background-color;
	padding-bottom: 120rpx;
}

.form-box {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;
}

.type-section {
	margin-bottom: 30rpx;

	.type-tabs {
		display: flex;
		border: 2rpx solid $theme-color;
		border-radius: 8rpx;
		overflow: hidden;

		.type-tab {
			flex: 1;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 28rpx;
			color: $theme-color;
			background-color: #fff;

			&.active {
				color: #fff;
				background-color: $theme-color;
			}
		}
	}
}

.item-section, .location-section {
	margin-bottom: 30rpx;
}

.form-item {
	margin-bottom: 30rpx;
	position: relative;

	.label {
		font-size: 28rpx;
		color: $text-color;
		margin-bottom: 20rpx;
		display: block;
	}

	.sub-label {
		font-size: 24rpx;
		color: #999;
		margin-left: 20rpx;
	}

	input, .picker, .location-select {
		width: 100%;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
		color: $text-color;
	}

	.picker, .location-select {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	textarea {
		width: 100%;
		height: 200rpx;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
	}

	.word-count {
		position: absolute;
		right: 20rpx;
		bottom: 20rpx;
		font-size: 24rpx;
		color: #999;
	}

	.price-input-box {
		display: flex;
		align-items: center;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;

		.currency {
			font-size: 32rpx;
			color: $text-color;
			margin-right: 10rpx;
		}

		.price-input {
			flex: 1;
			background-color: transparent;
			padding: 0;
		}
	}

	.reward-tip {
		font-size: 28rpx;
		color: #999;
		padding: 20rpx;
	}
}

.image-uploader {
	.image-list {
		display: flex;
		flex-wrap: wrap;
		margin-bottom: 20rpx;

		.image-item {
			width: 200rpx;
			height: 200rpx;
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			position: relative;

			image {
				width: 100%;
				height: 100%;
				border-radius: 8rpx;
			}

			.delete-btn {
				position: absolute;
				top: -20rpx;
				right: -20rpx;
				width: 40rpx;
				height: 40rpx;
				background-color: rgba(0, 0, 0, 0.5);
				color: #fff;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 32rpx;
			}
		}
	}

	.upload-btn {
		width: 200rpx;
		height: 200rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		text {
			font-size: 24rpx;
			color: #999;
			margin-top: 10rpx;
		}
	}
}

.submit-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: $theme-color;
	color: #fff;
	font-size: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
</style> 